﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
using System.Text;
using System.Collections;

namespace TagTesting
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        private List<Article> getArticleOnes(string  ids)
        {
            List<Article> getOnes = new List<Article>();
            using (DBTAGDataContext tagDB = new DBTAGDataContext())
            {
                string[] arrayID = ids.Split(',');
                List<int> arrayConvert = new List<int>();
                foreach (var item in arrayID)
                {
                    arrayConvert.Add(int.Parse(item));
                }
                   
                getOnes = tagDB.Articles.Where(i => arrayConvert.Contains(i.ArticleID)).ToList();
            }
            return getOnes;
        }
        private static string SplitTagCount(string resultTag)
        {
            if (resultTag.Contains('\\'))
            {
                string[] words = resultTag.Split('\\');
                SortedList wordList = new SortedList();
                int numWords = 0;
                foreach (string word in words)
                {
                    if (!(wordList.ContainsKey(word)))
                    {
                        wordList.Add(word, 1);
                        numWords++;
                    }
                    else
                    {
                        int iWordCount = (int)wordList[word];
                        wordList[word] = iWordCount + 1;
                    }
                }
                IDictionaryEnumerator enumerator = wordList.GetEnumerator();
                StringBuilder strResult = new StringBuilder();
                while (enumerator.MoveNext())
                {
                    if (wordList.Count <= 16)
                    {
                        strResult.Append(enumerator.Value.ToString() + ":" + enumerator.Key.ToString() + "/");
                    }
                    else
                    {
                        if ((int)enumerator.Value >= 2)
                            strResult.Append(enumerator.Value.ToString() + ":" + enumerator.Key.ToString() + "/");
                        else
                        {
                            if (WordCount(enumerator.Key.ToString()) > 3 || FirstUpperCase(enumerator.Key.ToString()))
                            {
                                strResult.Append(enumerator.Value.ToString() + ":" + enumerator.Key.ToString() + "/");
                            }
                        }
                    }
                }
                return strResult.ToString();
            }
            else
            {
                return resultTag;
            }
        }
        private static int WordCount(string listWords)
        {
            try
            {
                MatchCollection collection = Regex.Matches(listWords, @"[\S]+");
                return collection.Count;
            }
            catch
            {
                return 0;
            }
        }
        private static bool FirstUpperCase(string listWords)
        {
            bool isUpperCase = false;
            if (!listWords.Equals(string.Empty))
            {
                char CharUpperCase = Convert.ToChar(listWords.Substring(0, 1));
                if (char.IsUpper(CharUpperCase))
                    return true;
            }
            return isUpperCase;

        }

        protected void btgetTag_Click(object sender, EventArgs e)
        {
             var obj = getArticleOnes(idNew.Text);
            foreach (var item in obj)
            {
                string temContent = "";
                if (dlTypeGetContent.SelectedValue.Equals(""))
                    temContent = item.Title + item.Summary + item.FullContent;
                if (dlTypeGetContent.SelectedValue.Equals("1"))
                    temContent = item.Title;
                if (dlTypeGetContent.SelectedValue.Equals("2"))
                    temContent = item.Summary;
                if (dlTypeGetContent.SelectedValue.Equals("3"))
                    temContent = item.FullContent;
                if (dlTypeGetContent.SelectedValue.Equals("4"))
                    temContent = item.Title + item.Summary + item.FullContent;

                //Response.Write("<b style='color:red;'>decoder</b>   " + Server.HtmlDecode(temContent) + "<br/>");
                //Response.Write("<b style='color:red;'>encoder</b>   " + Server.HtmlEncode(temContent) + "<br/>");
                //Response.Write("<b style='color:red;'>strip html</b>   " + Server.HtmlEncode(StripHTML(temContent)) + "<br/>");
                try
                {
                    tagtest.HelloService ok = new tagtest.HelloService();
                    if (txtContentTemp.Text.Equals(string.Empty))
                    {
                        string resultOK = ok.getTagNPL(ConvertNewlinesToSingleSpaces(StripHTML(temContent)), 3);
                        string ok1 = SplitTagCount(resultOK);
                        Response.Write("<b style='color:red;'>result</b>" + item.ArticleID + ok1 + "<br/>" + ok1.Length);
                        resultOK = resultOK.Length >= 400 ? resultOK.Substring(0, 400) : resultOK;
                        Response.Write("<b style='color:red;'>text</b>" + resultOK + "<br/>");
                        Response.Write("<b style='color:red;'>result</b>" + item.ArticleID + SplitTagCount(resultOK) + "<br/>" + resultOK.Length);
                    }
                    else
                        Response.Write("<b style='color:red;'>result</b>" + ok.getTagNPL(txtContentTemp.Text, 3));
                }
                catch (Exception ex)
                {
                    Response.Write("<b style='color:red;'>" + ex.ToString() + "</b>");
                }

            }
        }
        private string StripHTML(string htmlString)
        {
            string pattern = @"<(.|\n)*?>";
            return Regex.Replace(htmlString, pattern, string.Empty);

        }
        public static string ConvertNewlinesToSingleSpaces(string value)
        {
            value = value.Replace("\r\n", " ");
            value = value.Replace('\n', ' ');
            return value;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
             string PrefixErr = "error";
            string ok = "error:" + "dang thành trung";
            Response.Write(ok.Substring(0, PrefixErr.Length));

        }
       
    }
}
